import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/education/custom_new_title_chart_page.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_time_period.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/utils/customEndDrawer.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/train_plan/get_education_plan_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/education/me_train_program/train_ing_program_controller.dart';

///培训计划(ing)
class TrainIngProgramView extends StatelessWidget {
  TrainIngProgramView({super.key});

  final controller = Get.put(TrainIngProgramController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.riskRightScaffoldKey,
            appBar: CustomAppBar(
              titleText: '培训计划',
            ),
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: _getSearchEnterWidget(),
                      ),
                      AppGaps.hGap8,
                      _screen(),
                      AppGaps.vGap10,
                    ],
                  ),
                  AppGaps.vGap12,
                  Expanded(
                      child: CustomEasyRefreshList<GetEducationPlanEntity>(
                    key: controller.globalKey,
                    dataLoad: controller.getEducationPlan,
                    itemBuilder: (item, index) {
                      return _itemWidget(item);
                    },
                  )),
                ],
              ),
            ), // 右侧筛选栏
            endDrawer:CustomEndDrawer(
              centerContext: Column(
                children: [
                  _selectType('培训类型', controller.trainingTypeBack),
                  _selectType('培训途径', controller.trainingChannelsBack),
                  _selectType('培训级别', controller.trainingLevelBack),
                  _alignTitle('培训时间'),
                  AppGaps.vGap16,
                  CustomSelectTimePeriod(
                    startTime: controller.startDate,
                    endTime: controller.endDate,
                    onConfirm: (start, end) {
                      controller.startDate = start;
                      controller.endDate = end;
                    },
                    showBorder: true,
                    iconCalendar: AppImages.icon__screen_clock,
                  ),
                ],
              ),
              reset: controller.reset,
              confirm: controller.confirm,
            ),
          );
        });
  }

  ///搜索框
  Widget _getSearchEnterWidget() {
    return Container(
        height: 38.w,
        padding: EdgeInsets.symmetric(horizontal: 12.w),
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: Colors.white, border: Border.all(color: const Color(0xffF0F0F0), width: 1.w), borderRadius: BorderRadius.all(Radius.circular(8.w))),
        child: Row(
          children: [
            SvgPicture.asset(
              AppImages.icon_search,
              width: 16.w,
            ),
            Expanded(
                child: TextField(
                    keyboardType: TextInputType.text,
                    textInputAction: TextInputAction.done,
                    onChanged: (value) {
                      controller.searchX = value;
                      controller.refreshPage();
                    },
                    style: TextStyle(color: const Color(0xff222222), fontWeight: FontWeight.w400, fontSize: 14.sp),
                    decoration: InputDecoration(
                      counterText: '',
                      //防止适配不居中
                      isCollapsed: true,
                      border: const OutlineInputBorder(borderSide: BorderSide.none),
                      contentPadding: EdgeInsets.only(left: 8.w),
                      hintText: '请输入调研名称搜索',
                      hintStyle: TextStyle(fontSize: 14.sp, fontWeight: FontWeight.w400, color: const Color(0xffA0A7B5)),
                    )))
          ],
        ));
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 38.w,
      height: 38.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).click(controller.onFilterButtonPressed);
  }

  Widget _rowData(String name, String value, {Alignment alignment = Alignment.centerLeft}) {
    return Row(
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 13.sp,
          ),
        ),
        Expanded(
          child: Align(
            alignment: alignment,
            child: Text(
              value,
              style: TextStyle(
                color: const Color(0xFF495061),
                fontSize: 12.sp,
                fontWeight: FontWeight.w400,
              ),
            ),
          ),
        )
      ],
    );
  }

  Widget _selectType(String firstName, String name) {
    return Column(
      children: [
        _alignTitle(firstName),
        AppGaps.vGap16,
        Container(
          height: 38.w,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(8),
            border: Border.all(width: 1.w, color: const Color(0xFFE3E7EE)),
          ),
          child: Row(
            children: [
              AppGaps.hGap20,
              Expanded(
                child: Text(
                  name,
                  style: TextStyle(
                    fontSize: 12.sp,
                    color: const Color(0xFF495061),
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
              SvgPicture.asset(AppImages.icon_filter_right_arrow),
              AppGaps.hGap20,
            ],
          ),
        ).clickOnce(() {
          if (firstName == '培训类型') {
            controller.selectPxlxJy31TrQXType();
          } else if (firstName == '培训级别') {
            controller.selectPxfsVSWR8mRxType();
          } else if (firstName == '培训途径') {
            controller.selectCurrencyTrainingChannels();
          } else if (firstName == '') {
          } else {}
        }),
        AppGaps.vGap16,
      ],
    );
  }

  //	审核状态 0:待提交 1:进行中 2:已结束 //0：待发起 1：待审批 2：审批中 3：通过 4：驳回
  String _strStatus(String? status) {
    switch (status) {
      case '0':
        return '待发起';
      case '1':
        return '待审批';
      case '2':
        return '审批中';
      case '3':
        return '通过';
      case '4':
        return '驳回';
      default:
        return '';
    }
  }
  //	审核状态 0:待提交 1:进行中 2:已结束//0：待发起 1：待审批 2：审批中 3：通过 4：驳回
  Color _strStatusColors(String? statusColors) {
    switch (statusColors) {
      case '0':
        return const Color(0xFFFC9900);
      case '1':
        return const Color(0xFFA5A9C8);
      case '2':
        return const Color(0xFFA5A9C8);
      case '3':
        return const Color(0xFF00CE78);
      case '4':
        return const Color(0xFFDE0F0F);
      default:
        return const Color(0xFFA5A9C8);
    }
  }

  /// 获取审批按钮
  Widget _getApprovalButton(GetEducationPlanEntity item) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          '操作:',
          style: TextStyle(
            fontSize: 13.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: const Color(0xffA5A6B5),
          ),
        ),
        Container(
          padding: EdgeInsets.fromLTRB(10.w, 6.w, 6.w, 6.w),
          decoration: BoxDecoration(
            color: const Color(0xFFE5E7FF),
            borderRadius: BorderRadius.circular(4.w),
          ),
          child: Row(
            children: [
              Text(
                '审批',
                style: TextStyle(
                  fontSize: 13.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xff5C6DFF),
                ),
              ),
              AppGaps.hGap2,
              SvgPicture.asset(
                AppImages.icon_right_arrow,
              ),
            ],
          ),
        )
      ],
    ).clickOnce(() {
      //进入审批界面
      controller.onClickApprove(item);
    });
  }

  Widget _itemWidget(GetEducationPlanEntity item) {
    return CustomNewTitleChartPage(
      title: '${item.planName}',
      titleRightWidget: Container(
        width: 52.w,
        height: 18.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: _strStatusColors(item.status),
            borderRadius: const BorderRadius.only(
              topLeft: Radius.circular(10),
              topRight: Radius.circular(10),
              bottomRight: Radius.circular(10),
            )),
        child: Text(
          _strStatus(item.status),
          style: TextStyle(
            color: Colors.white,
            fontSize: 10.sp,
            fontWeight: FontWeight.w400,
          ),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _rowData('培训时间：', item.startTime ?? '', alignment: Alignment.centerLeft),
          AppGaps.vGap12,
          _rowData('培训类型：', item.trainingTypeName ?? '', alignment: Alignment.centerLeft),
          AppGaps.vGap12, //培训途径 1:线上 2:线下
          _rowData('培训途径：', item.trainingChannels == '1' ? '线上' : '线下', alignment: Alignment.centerLeft),
          AppGaps.vGap12,
          Row(
            children: [
              Expanded(
                child: _rowData('培训级别：', item.trainingLevelName ?? '', alignment: Alignment.centerLeft),
              ),
              if(item.status=='3') //0：待发起 1：待审批 2：审批中 3：通过 4：驳回
              Visibility(
                  visible: item.trainingChannels == '1' ? true : false,
                  child: Container(
                    padding: EdgeInsets.only(left: 14.w, right: 14.w, top: 2.w, bottom: 2.w),
                    decoration: BoxDecoration(
                      color: Colors.blue,
                      borderRadius: BorderRadius.all(Radius.circular(8.r)),
                    ),
                    child: Text(
                      '进入',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 14.w,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ).clickOnce(() {
                    controller.inputClickPage(item);
                  }))
            ],
          ),
          if(item.status=='1') //0：待发起 1：待审批 2：审批中 3：通过 4：驳回
          Column(
            children: [
              AppGaps.vGap12,
              _getApprovalButton(item),
            ],
          )
        ],
      ),
    ).click(() {
      controller.onClickButtonPressed(item);
    });
  }

  Widget _alignTitle(String title) {
    return  Align(
      alignment: Alignment.centerLeft,
      child: Text(
        title,
        style: TextStyle(
          fontSize: 14.sp,
          fontWeight: FontWeight.w400,
          color: const Color(0xFF262F38),
        ),
      ),
    );
  }
}

